package com.java.myapp;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener

{

    public static void main(String arr[])

    {

        new Login();

    }

    JLabel l1, l2, l3;

    JTextField tf1;

    JButton btn1;

    JPasswordField p1;

 

    Login()

    {

        setTitle("CalMe");

        setVisible(true);

        setSize(571, 276);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        l1 = new JLabel("Login Form in Windows Form:");
        l1.setBounds(100, 30, 400, 30);

        l1.setForeground(Color.blue);

        l1.setFont(new Font("Serif", Font.BOLD, 20));

 

        l2 = new JLabel("Username:");
        l2.setBounds(80, 70, 200, 30);

        l3 = new JLabel("Password:");
        l3.setBounds(80, 110, 200, 30);

        tf1 = new JTextField();
        tf1.setBounds(300, 70, 200, 30);

        p1 = new JPasswordField();
        p1.setBounds(300, 110, 200, 30);

        btn1 = new JButton("Submit");
        btn1.setBounds(257, 161, 100, 30);
        getContentPane().setLayout(null);

 

        getContentPane().add(l1);

        getContentPane().add(l2);

        getContentPane().add(tf1);

        getContentPane().add(l3);

        getContentPane().add(p1);

        getContentPane().add(btn1);
        
        JButton btnRegister = new JButton("register");
        btnRegister.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		
        		////////////////////////////////////////////////////////////////////////
        	}
        });
        btnRegister.setBounds(117, 161, 89, 30);
        getContentPane().add(btnRegister);

        btn1.addActionListener(this);

    }

 

    public void actionPerformed(ActionEvent e)

    {
        showData();
    }

 

    public void showData()

    {

        JFrame f1 = new JFrame();

        JLabel l, l0;

 

        String str1 = tf1.getText();

        char[] p = p1.getPassword();

        String str2 = new String(p);

        try

        {

            Class.forName("com.mysql.jdbc.Driver");

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
					"?user=root&password=root");

            PreparedStatement ps = con.prepareStatement(" SELECT * FROM  member " +
					" WHERE Username = ? " +
					" AND Password = ? ");

            ps.setString(1, str1);

            ps.setString(2, str2);

            ResultSet rs = ps.executeQuery();

            if (rs.next())

            {

                f1.setVisible(true);

                f1.setSize(600, 600);

                f1.getContentPane().setLayout(null);

                l = new JLabel();

                l0 = new JLabel("you are succefully logged in..");

                l0.setForeground(Color.blue);

                l0.setFont(new Font("Serif", Font.BOLD, 30));

                l.setBounds(60, 50, 400, 30);

                l0.setBounds(60, 100, 400, 40);

 

                f1.getContentPane().add(l);

                f1.getContentPane().add(l0);

                l.setText("Welcome " + rs.getString(1));

                l.setForeground(Color.red);

                l.setFont(new Font("Serif", Font.BOLD, 30));

 

            } else

            {

                JOptionPane.showMessageDialog(null,

                   "Incorrect username or password...Try Again with correct detail");

            }

        } 

        catch (Exception ex)

        {

            System.out.println(ex);

        }

    }

} 